﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XDYRestaurant
{
    public partial class Payment : Form
    {
        private Context.Context ctx = new Context.Context();
        private static Payment _Payment;
        private static int index;
        private static double sum;
        public Payment()
        {
            InitializeComponent();
        }
        public static Payment Create(int index1)
        {
            index = index1;
            if (_Payment != null)
            {
                _Payment.Close();
            }
            _Payment = new Payment();
            return _Payment;
        }
        private void Payment_Load(object sender, EventArgs e)
        {
            dgvDishInfoList2_show();
        }

        public void dgvDishInfoList2_show()
        {
            sum = 0;
            List<New_List> list = new List<New_List>();
            var listOrder = ctx.Orders.Where(s => s.TableId == index).ToList();
            foreach (var item in listOrder)
            {
                var tmp = ctx.Cookbooks.FirstOrDefault(s => s.CookId == item.CookId);
                New_List one = new New_List();
                one.OrderId = item.OrderId;
                one.CookId = tmp.CookId;
                one.CookName = tmp.CookName;
                one.CookPrice = (double)tmp.CookPrice;
                sum += (double)tmp.CookPrice;
                one.CookSpecies = tmp.CookSpecies;
                list.Add(one);
            }
            dgvDishInfoList2.DataSource = list;
            sum = Math.Round(sum, 2);
            lblCostMoney.Text = sum.ToString();
            lblGetMoney.Text = "0";
            lblRetuenMoney.Text = sum.ToString();
            txtPhone.Text = ctx.Tables.FirstOrDefault(s => s.TableId == index).CustomerPhone;
            txtName.Text = ctx.Customers.FirstOrDefault(s => s.CustomerPhone == txtPhone.Text).CustomerName;
            txtMoney.Text = "0";
        }
        private class New_List
        {
            int orderId;
            string cookId;
            string cookName;
            string cookSpecies;
            double cookPrice;

            public int OrderId { get => orderId; set => orderId = value; }
            public string CookId { get => cookId; set => cookId = value; }
            public string CookName { get => cookName; set => cookName = value; }
            public string CookSpecies { get => cookSpecies; set => cookSpecies = value; }
            public double CookPrice { get => cookPrice; set => cookPrice = value; }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            _Payment.Close();
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            Models.Table table = new Models.Table();
            Models.Customer customer = new Models.Customer();
            customer = ctx.Customers.FirstOrDefault(s => s.TableId == index);
            if(txtPassword.Text != customer.CustomerPassword)
            {
                MessageBox.Show("密令错误！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if(lblRetuenMoney.Text == "金额不足"|| lblRetuenMoney.Text == "格式错误")
            {
                MessageBox.Show(lblRetuenMoney.Text+"!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var listOrder = ctx.Orders.Where(s => s.TableId == index).ToList();
            foreach (var item in listOrder)
            {
                if (item.CookStatic == 0) continue;
                Models.Bill bill = new Models.Bill();
                bill.CookId = item.CookId;
                bill.CustomerPhone = item.CustomerPhone;
                bill.BillTime = DateTime.Now.ToLocalTime();
                bill.CookPrice = (double)ctx.Cookbooks.FirstOrDefault(s => s.CookId == item.CookId).CookPrice;
                bill.TableId = item.TableId;
                ctx.Bills.Add(bill);
                Models.Order order = item;
                ctx.Orders.Remove(order);
            }
            table = ctx.Tables.FirstOrDefault(s => s.TableId == index);
            table.CustomerPhone = null;
            table.TableStatic = 0;
            customer.TableId = null;
            ctx.SaveChanges();
            dgvDishInfoList2_show();
            MessageBox.Show("结算成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            double f;
            if (!double.TryParse(txtMoney.Text, out f))
            {
                lblGetMoney.Text = "格式错误";
                lblRetuenMoney.Text = "格式错误";
            }
            else
            {
                double tmp = double.Parse(txtMoney.Text);
                tmp = Math.Round(tmp, 2);
                lblGetMoney.Text = tmp.ToString();
                lblRetuenMoney.Text = Math.Round(tmp - sum, 2).ToString();
            }
            if(lblRetuenMoney.Text[0] == '-')
            {
                lblRetuenMoney.Text = "金额不足";
            }
        }
    }
}
